package website;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import util.DateUtil;
import util.StringUtil;

import common.serialization.IWebText;
import common.serialization.StringWebText;
import common.serialization.XmlDataBean;
import common.service.BaseService;
import common.service.CacheService;
import common.service.IService;
import common.service.ServiceFactory;
import common.web.WebHelper;
import common.web.action.BaseAction;

/**
 * 
 * 网站管理页面控制类
 *
 */
public class WebSiteAction {
	
	//获取第一层的栏目
	public static List<Map<String,Object>> getFirstColumn(HttpServletRequest request) throws Exception {
		IService service = new BaseService();
		List<Map<String, Object>> list = service.queryBySql("select * from WEBSITE_COLUMN where ENABLED=1 and PID='0' and PUBLISHED=1 order by TREE_ORDER ");
		request.setAttribute("firstColumnList", list);
		return list;
	}
	
	//获取顶部栏目信息
	public static List<Map<String,Object>> getTopColumn(HttpServletRequest request) throws Exception {
		Map<String,Object> pmap = CacheService.getDataById("SYSMAN_DD", "CODE", "WEBSITE_COLUMN_TOP");
		
		List<Map<String,Object>> topColumnList = CacheService.getDataByCacheId("SYSMAN_DD", "PID", (String)pmap.get("ID"));
		request.setAttribute("topColumnList", topColumnList);
		return topColumnList;	
	}
	
	//获取底部栏目信息
	public static List<Map<String,Object>> getBottomColumn(HttpServletRequest request) throws Exception {
		Map<String,Object> pmap = CacheService.getDataById("SYSMAN_DD", "CODE", "WEBSITE_COLUMN_BOTTOM");
		
		List<Map<String,Object>> bottomColumnList = CacheService.getDataByCacheId("SYSMAN_DD", "PID", (String)pmap.get("ID"));
		request.setAttribute("bottomColumnList", bottomColumnList);
		return bottomColumnList;	
	}
	
	//根据菜单ID获取栏目信息
	public static Map<String,Object> getColumnById(HttpServletRequest request) throws Exception {
		Map<String,Object> menuColumn = null;
		String menuId = request.getParameter("menuId");//菜单ID,即第一层栏目ID
		if(StringUtils.isEmpty(menuId)) {
			List<Map<String,Object>> firstColumnList = getFirstColumn(request);
			menuColumn = firstColumnList.get(0);
		} else {
			IService service = new BaseService();
			menuColumn = service.queryById("select * from WEBSITE_COLUMN where ENABLED=1 and ID='" + menuId + "'");
		}
		request.setAttribute("menuColumn", menuColumn);
		request.setAttribute("menuId", menuId);
		return menuColumn;
	}
	
	//根据文章ID获取文章信息
	public static Map<String,Object> getArticleById(HttpServletRequest request) throws Exception {
		String articleId = request.getParameter("columnId");//左边栏目ID
		IService service = new BaseService();
		if(StringUtils.isEmpty(articleId)) {//获取菜单栏目下的第一篇文章
			String columnId = request.getParameter("menuId");
			List<Map<String,Object>> articleList = service.queryBySql("select * from WEBSITE_ARTICLE where ENABLED=1 and PUBLISHED=1 and COLUMN_ID='" + columnId + "' order by PUBLISH_TIME desc ");
			if(articleList != null && !articleList.isEmpty()) {
				request.setAttribute("article", articleList.get(0));
			}
		} else {
			Map<String,Object> map = service.queryById("select * from WEBSITE_ARTICLE where ENABLED=1 and PUBLISHED=1 and ARTICLE_ID='" + articleId + "'");
			request.setAttribute("article", map);
		}
		return null;
	}
	
	//获取首页栏目信息
	public static void getIndexColumn(HttpServletRequest request) throws Exception {
		IService service = ServiceFactory.getService(null);
		
		//中间栏目
		Map<String,Object> centerColumn = CacheService.getDataById("SYSMAN_DD", "CODE", "WEBSITE_INDEX_CENTER");
		String columnId = (String)centerColumn.get("REMARK");
		request.setAttribute("centerColumnId", columnId);
		List<Map<String,Object>> centerColumnList = service.queryBySql("select * from WEBSITE_ARTICLE where ENABLED=1 and PUBLISHED=1 and COLUMN_ID='" + columnId + "' order by PUBLISH_TIME desc limit 0,5 ");
		request.setAttribute("centerColumnList", centerColumnList);
		
		//右边栏目
		Map<String,Object> rightColumn = CacheService.getDataById("SYSMAN_DD", "CODE", "WEBSITE_INDEX_RIGHT");
		columnId = (String)rightColumn.get("REMARK");
		request.setAttribute("rightColumnId", columnId);
		List<Map<String,Object>> rightColumnList = service.queryBySql("select * from WEBSITE_ARTICLE where ENABLED=1 and PUBLISHED=1 and COLUMN_ID='" + columnId + "' order by PUBLISH_TIME desc limit 0,5 ");
		request.setAttribute("rightColumnList", rightColumnList);
	}
	
	//根据栏目ID获取文章信息
	public static List<Map<String,Object>> getArticleListByColumnId(HttpServletRequest request) throws Exception {
		String columnId = request.getParameter("columnId");//左边点击的栏目ID
		if(!StringUtils.isEmpty(columnId)) {
			IService service = new BaseService();
			List<Map<String,Object>> articleList = service.queryBySql("select * from WEBSITE_ARTICLE where ENABLED=1 and PUBLISHED=1 and COLUMN_ID='" + columnId + "' order by PUBLISH_TIME desc ");
			request.setAttribute("articleList", articleList);
			return articleList;
		} 
	
		return new ArrayList<Map<String,Object>>();
	}
	
	//根据菜单ID获取所有子栏目
	public static XmlDataBean getChildColumnByPid(HttpServletRequest request)throws Exception {
		String pid = request.getParameter("menuId");

		XmlDataBean root = new XmlDataBean();
		root.setNameSpace("tree");
		root.setId(pid);
		root.setAttribute("type", "column");//栏目类型

		IService service = ServiceFactory.getService(null);
		List<Map<String, Object>> list = service.queryBySql("select * from WEBSITE_COLUMN where ENABLED=1 and PUBLISHED=1 and ID<>'" + pid + "' and PATH like '%" + pid + "%' order by PATH ");
		if(list == null || list.isEmpty()) {//查询该栏目下的所有文章
			Map<String,Object> menuColumn = (Map<String,Object>)request.getAttribute("menuColumn");
			String url = (String)menuColumn.get("URL");
			url = (url == null) ? "" : url;
			
			list = service.queryBySql("select * from WEBSITE_ARTICLE where ENABLED=1 and PUBLISHED=1 and COLUMN_ID='" + pid + "' order by PUBLISH_TIME desc ");
			
			root.setAttribute("type", "article");//文章类型
			for (Map<String, Object> map : list) {
				String id = (String)map.get("ARTICLE_ID");
				
				XmlDataBean child = new XmlDataBean();
				child.setNameSpace("item");
				child.setId(id);
				child.setAttribute("text", (String)map.get("ARTICLE_TITLE"));
				child.setAttribute("url",url);
				
				//加到父节点
				root.appendChildXmlDataBean(child);
			}
		} else {//构造树型栏目数据
			for (Map<String, Object> map : list) {
				String id = (String)map.get("ID");
				String PID = (String)map.get("PID");
				String url = (String)map.get("URL");
				url = (url == null) ? "" : url;
				
				XmlDataBean child = new XmlDataBean();
				child.setNameSpace("item");
				child.setId(id);
				child.setAttribute("text", (String)map.get("NAME"));
				child.setAttribute("url", url);
				
				//加到父节点
				root.findXmlDataBean(PID).appendChildXmlDataBean(child);
			}
		}
		request.setAttribute("childColumn", root);
		return root;
	}
	
	//获取网站相关配置信息
	public static Map<String,Object> getWebsiteConfig(HttpServletRequest request) throws Exception {
		Map<String,Object> configMap = new HashMap<String, Object>();
		Map<String,Object> pmap = CacheService.getDataById("SYSMAN_DD", "CODE", "WEBSITE_CONFIG");
		
		List<Map<String,Object>> configList = CacheService.getDataByCacheId("SYSMAN_DD", "PID", (String)pmap.get("ID"));
		for(Map<String,Object> map : configList) {
			String code = (String)map.get("CODE");
			String value = (String)map.get("REMARK");
			configMap.put(code, value);
		}
		request.setAttribute("configMap", configMap);
		return configMap;
		
	}
	
	//发布栏目
	public IWebText publishColumn(WebHelper webHelper) throws Exception {
		StringWebText swt = new StringWebText();
		swt.setValue("0");
		
		HttpServletRequest request = webHelper.getRequest();
		String ids = request.getParameter("ids");
		String sql = "update WEBSITE_COLUMN set PUBLISHED=1 , PUBLISH_TIME='" + DateUtil.dateToString(new Date()) + "' where ID in (" + StringUtil.getMoreIdCond(ids) + ")";
		IService service = new BaseService();
		int size = service.executeBySql(sql);
		if(size > 0) {
			swt.setValue("1");
		}
		return swt;
	}
	
	//取消发布栏目
	public IWebText cancelpublishColumn(WebHelper webHelper) throws Exception {
		StringWebText swt = new StringWebText();
		swt.setValue("0");
		
		HttpServletRequest request = webHelper.getRequest();
		String ids = request.getParameter("ids");
		String sql = "update WEBSITE_COLUMN set PUBLISHED=0 , PUBLISH_TIME=null where ID in (" + StringUtil.getMoreIdCond(ids) + ")";
		IService service = new BaseService();
		int size = service.executeBySql(sql);
		if(size > 0) {
			swt.setValue("1");
		}
		return swt;
	}
	
	//发布文章
	public IWebText publishArticle(WebHelper webHelper) throws Exception {
		StringWebText swt = new StringWebText();
		swt.setValue("0");
		
		HttpServletRequest request = webHelper.getRequest();
		String ids = request.getParameter("ids");
		String sql = "update WEBSITE_ARTICLE set PUBLISHED=1 , PUBLISH_TIME='" + DateUtil.dateToString(new Date()) + "' where ARTICLE_ID in (" + StringUtil.getMoreIdCond(ids) + ")";
		IService service = new BaseService();
		int size = service.executeBySql(sql);
		if(size > 0) {
			swt.setValue("1");
		}
		return swt;
	}
	
	//取消发布文章
	public IWebText cancelpublishArticle(WebHelper webHelper) throws Exception {
		StringWebText swt = new StringWebText();
		swt.setValue("0");
		
		HttpServletRequest request = webHelper.getRequest();
		String ids = request.getParameter("ids");
		String sql = "update WEBSITE_ARTICLE set PUBLISHED=0 , PUBLISH_TIME=null where ARTICLE_ID in (" + StringUtil.getMoreIdCond(ids) + ")";
		IService service = new BaseService();
		int size = service.executeBySql(sql);
		if(size > 0) {
			swt.setValue("1");
		}
		return swt;
	}
	
	//客户提交留言
	public IWebText doSaveMsg(WebHelper webHelper) throws Exception {
		StringWebText swt = new StringWebText();
		swt.setValue("0");
		
		HttpServletRequest request = webHelper.getRequest();
		try {
			BaseAction.doSave(request);
			swt.setValue("1");
		} catch(Exception e) {
			e.printStackTrace();
		}
		return swt;
	}
	
	
}
